load("//tools/skylark:drake_py.bzl", "drake_py_unittest")
load("//tools/skylark:py.bzl", "py_library")
load("//tools/lint:lint.bzl", "add_lint_tests")
load("@python//:version.bzl", "PYTHON_SITE_PACKAGES_RELPATH")
load("//tools/install:install.bzl", "install", "install_files")
load(
    "//tools/workspace:cmake_configure_file.bzl",
    "cmake_configure_file",
)
load("//tools/skylark:test_tags.bzl", "vtk_test_tags")
load(
    "@drake_visualizer//:defs.bzl",
    _DRAKE_VISUALIZER_ENABLED = "ENABLED",
)

exports_files(
    # To be consumed by `//tools:drake_visualizer_py`.
    [
        "drake_visualizer_bazel.py",
        "drake_visualizer_disabled.py",
    ],
    visibility = ["//tools:__pkg__"],
)

# TODO(eric.cousineau): Merge this with `drake_visualizer_py` once it moves
# from `//tools` to here.
py_library(
    name = "builtin_scripts_py",
    visibility = ["//tools:__pkg__"],
    deps = [
        "//tools/workspace/drake_visualizer/_drake_visualizer_builtin_scripts",
    ],
)

# TODO(eric.cousineau): Use something simpler than cmake_configure_file to do
# basic substitutions as a genrule.
cmake_configure_file(
    name = "config_wrapper_script",
    src = "drake_visualizer_installed.py.in",
    out = "drake_visualizer_installed.py",
    defines = [
        "PYTHON_SITE_PACKAGES_RELPATH=" + PYTHON_SITE_PACKAGES_RELPATH,
    ],
)

install_files(
    name = "install_wrapper_script",
    dest = "bin",
    tags = [
        # In the case when drake-visualizer is disabled, this target will not
        # participate in the install target.  Nerf the linter warning for that.
        "no_install_lint",
    ],
    files = [
        "drake_visualizer_installed.py",
    ] if _DRAKE_VISUALIZER_ENABLED else [],
    rename = {
        "bin/drake_visualizer_installed.py": "drake-visualizer",
    },
)

install(
    name = "install",
    install_tests = [
        ":test/drake_visualizer_installed_test.py",
    ] if _DRAKE_VISUALIZER_ENABLED else [],
    visibility = ["//tools/workspace:__pkg__"],
    deps = [
        ":install_wrapper_script",
        "//tools/workspace/drake_visualizer/_drake_visualizer_builtin_scripts:install",  # noqa
        "@drake_visualizer//:install",
    ],
)

drake_py_unittest(
    name = "drake_visualizer_bazel_test",
    args = [] if _DRAKE_VISUALIZER_ENABLED else [
        "TestDrakeVisualizerBazel.test_help_basic",
    ],
    data = [
        "//tools:drake_visualizer",
    ],
    tags = vtk_test_tags(),
)

add_lint_tests(
    python_lint_extra_srcs = [
        "drake_visualizer_bazel.py",
        "drake_visualizer_disabled.py",
        "drake_visualizer_installed.py",
    ],
)
